package com.ljm.boot.helloword.letCode;

import java.util.HashMap;
import java.util.Map;

public class Test03_todo {
    public static void main(String[] args) {
        String  s =  "abcdabbcabc";
        lengthOfLongestSubstring(s);
        }
    public static  int  lengthOfLongestSubstring2(String s) {
        int length = 0;
        for (int i= 0;i<s.length();i++){
            String ab = String.valueOf(s.charAt(i));

            for (int j=i+1;j<s.length();j++) {
                ab = ab+String.valueOf(s.charAt(j));
                String last = s.substring(j+1,s.length());
                if(last.indexOf(ab)!=-1&&ab.length()>length){
                    length=ab.length();
                }
            }
        }
        System.out.println(length);
        return length;
    }

        public static  int  lengthOfLongestSubstring(String s) {
            Map<Character, Integer> dic = new HashMap<>();
            int i = -1, res = 0, len = s.length();
            for(int j = 0; j < len; j++) {
                if (dic.containsKey(s.charAt(j)))
                    i = Math.max(i, dic.get(s.charAt(j))); // 更新左指针 i
                dic.put(s.charAt(j), j); // 哈希表记录
                res = Math.max(res, j - i); // 更新结果
            }
            System.out.println(res);
            return res;
        }
    }


